<template>
  <div class="login-page">
    <!-- 左侧装饰区域 -->
    <div class="login-left">
      <div class="brand-container">
        <n-icon size="64" color="#fff">
          <Settings />
        </n-icon>
        <h1 class="brand-name">设备命令管理系统</h1>
        <p class="brand-desc">高效、安全、专业的企业管理平台</p>
      </div>
    </div>
    
    <!-- 右侧登录区域 -->
    <div class="login-right">
      <div class="login-form-container">
        <h2 class="welcome-text">欢迎登录</h2>
        <p class="welcome-desc">请使用您的账号和密码登录系统</p>
        
        <!-- 登录表单 -->
        <n-form
          ref="formRef"
          :model="formData"
          :rules="rules"
          label-placement="top"
          size="large"
          class="login-form"
        >
          <n-form-item path="username">
            <template #label>
              <span class="label-text">用户名</span>
            </template>
            <n-input 
              v-model:value="formData.username" 
              placeholder="请输入用户名"
              class="login-input"
            >
              <template #prefix>
                <n-icon><PersonOutline /></n-icon>
              </template>
            </n-input>
          </n-form-item>
          
          <n-form-item path="password">
            <template #label>
              <span class="label-text">密码</span>
            </template>
            <n-input
              v-model:value="formData.password"
              type="password"
              placeholder="请输入密码"
              show-password-on="click"
              class="login-input"
            >
              <template #prefix>
                <n-icon><LockClosedOutline /></n-icon>
              </template>
            </n-input>
          </n-form-item>
          
          <div class="login-options">
            <n-checkbox v-model:checked="rememberMe">记住我</n-checkbox>
            <a class="forgot-password" @click="handleForgotPassword">忘记密码？</a>
          </div>
          
          <n-button
            type="primary"
            block
            @click="handleLogin"
            :loading="loading"
            class="login-button"
          >
            登录
          </n-button>
        </n-form>
        
        <!-- 页脚版权信息 -->
        <div class="login-footer">
          <p>Copyright © 2025 设备命令管理系统 All Rights Reserved</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { 
  useMessage,
  NInput, 
  NButton, 
  NForm, 
  NFormItem,
  NCheckbox,
  NIcon 
} from 'naive-ui';
import { login } from '@/api/user';
import { 
  PersonOutline, 
  LockClosedOutline,
  Settings
} from '@vicons/ionicons5';

const router = useRouter();
const message = useMessage();
const formRef = ref(null);
const loading = ref(false);
const rememberMe = ref(false);

const formData = reactive({
  username: '',
  password: ''
});

// 页面加载时检查是否有保存的用户名
onMounted(() => {
  const savedUsername = localStorage.getItem('remember_username');
  if (savedUsername) {
    formData.username = savedUsername;
    rememberMe.value = true;
  }
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
};

const handleLogin = (e) => {
  e.preventDefault();
  formRef.value?.validate(async (errors) => {
    if (!errors) {
      loading.value = true;
      try {
        // 调用登录API
        const res = await login({
          username: formData.username,
          password: formData.password
        });
        
        // 检查res是否存在及其格式
        console.log('登录响应:', res);
        
        if (res && res.code === 200 && res.data) {
          const token = res.data.token;
          localStorage.setItem('token', token);
          if (rememberMe.value) {
            localStorage.setItem('remember_username', formData.username);
          } else {
            localStorage.removeItem('remember_username');
          }
          message.success('登录成功');
          router.push('/');
        } else {
          message.error(res?.message || '登录失败，请检查用户名和密码');
        }
      } catch (error) {
        console.error('登录错误:', error);
        message.error('登录失败: ' + (error?.message || '服务器错误'));
      } finally {
        loading.value = false;
      }
    } else {
      message.error('请填写完整的登录信息');
    }
  });
};

const handleForgotPassword = () => {
  message.info('请联系系统管理员重置密码');
};
</script>

<style scoped>
.login-page {
  display: flex;
  min-height: 100vh;
  background-color: #f5f7fa;
  width: 100%;
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

/* 左侧品牌区域 */
.login-left {
  flex: 0 0 40%;
  background: linear-gradient(135deg, #36d1dc, #5b86e5);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.1);
  transform: rotate(30deg);
}

.brand-container {
  position: relative;
  z-index: 1;
  text-align: center;
  color: white;
}

.brand-name {
  font-size: 32px;
  font-weight: bold;
  margin: 20px 0 10px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.brand-desc {
  font-size: 16px;
  opacity: 0.9;
  max-width: 300px;
  margin: 0 auto;
}

/* 右侧登录区域 */
.login-right {
  flex: 0 0 60%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
}

.welcome-text {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.welcome-desc {
  font-size: 16px;
  color: #888;
  margin-bottom: 30px;
}

.login-form {
  margin-bottom: 30px;
}

.label-text {
  font-size: 14px;
  font-weight: 500;
  color: #666;
}

.login-input {
  margin-bottom: 5px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0 24px;
}

.forgot-password {
  color: #5b86e5;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #36d1dc;
  text-decoration: underline;
}

.login-button {
  height: 46px;
  font-size: 16px;
  background: linear-gradient(to right, #36d1dc, #5b86e5);
  border: none;
  transition: all 0.3s;
}

.login-button:hover {
  opacity: 0.9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(91, 134, 229, 0.4);
}

.login-footer {
  text-align: center;
  color: #999;
  font-size: 13px;
  margin-top: 30px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-page {
    flex-direction: column;
  }
  
  .login-left {
    flex: 0 0 auto;
    padding: 30px 20px;
  }
  
  .login-right {
    flex: 1;
    padding: 30px 20px;
  }
  
  .brand-name {
    font-size: 24px;
  }
}
</style> 